package com.example.martian.service;

import com.example.martian.mapper.MessageMapper;
import com.example.martian.utils.Utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional(readOnly = true)
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    //查询所有公告
    @Transactional
    public Object selectNoticeAll(){
        JSONObject jsonObject = null;
        JSONArray jsonArray = new JSONArray();
        List<Map<String, Object>> maps = messageMapper.selectNoticeAll();
        for (Map<String,Object> map : maps){
            jsonObject = new JSONObject();
            jsonObject.put("noticeId",map.get("cmc_id"));
            jsonObject.put("subtitle",map.get("cmc_subtitle"));
            jsonObject.put("describe",map.get("cmc_describe"));
            jsonObject.put("creatTime",map.get("creatTime"));
            jsonObject.put("zt",map.get("cmc_zt"));
            jsonArray.add(jsonObject);
        }
        return Utils.toJSON(jsonArray.toString(),"0");
    }

    //根据id查询公告
    @Transactional
    public Object selectNoticeById(Integer noticeId){
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        List<Map<String, Object>> maps = messageMapper.selectNoticeById(noticeId);
        for (Map<String,Object> map : maps){
            jsonObject.put("subtitle",map.get("cmc_subtitle"));
            jsonObject.put("describe",map.get("cmc_describe"));
            jsonObject.put("creatTime",map.get("creatTime"));
            jsonObject.put("zt",map.get("cmc_zt"));
            jsonArray.add(jsonObject);
        }
        return Utils.toJSON(jsonArray.toString(),"0");
    }
}
